package com.xxxx.yebserver.service.impl;

import com.xxxx.yebserver.constant.RedisContant;
import com.xxxx.yebserver.entity.Menu;
import com.xxxx.yebserver.entity.Role;
import com.xxxx.yebserver.mapper.RoleMapper;
import com.xxxx.yebserver.pojo.MySecurityUser;
import com.xxxx.yebserver.service.RoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxxx.yebserver.vo.MenusWithRoleVo;
import com.xxxx.yebserver.vo.RoleWithMenuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cxq
 * @since 2021-06-15
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
    @Cacheable(value = "role",key = "#root.methodName+':'+#adminId")
    @Override
    public List<Role> getRoles(Long adminId) {
        return baseMapper.getRoles(adminId);
    }


    @Override
    public List<RoleWithMenuVo> getRoleWithMenu(Long rid) {
        return baseMapper.getRoleWithMenu(rid);
    }

    @Override
    public List<Long> getRoleWithMenuById(Long rid) {
        List<RoleWithMenuVo> roleWithMenu = getRoleWithMenu(rid);
        if(roleWithMenu == null || roleWithMenu.size() == 0) return null;
        RoleWithMenuVo vo = roleWithMenu.get(0);
        List<Menu> menus = vo.getMenus();
        if(menus == null || menus.size() == 0) return null;
        return menus.stream().map(item->item.getMenuId()).collect(Collectors.toList());
    }
}
